package es.gesfutbol.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.Order;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import es.gesfutbol.dao.IQuinielaDao;
import es.gesfutbol.domini.implementacio.Quiniela;
import es.gesfutbol.web.backbean.EscrutiniBackBean;


public class EscrutiniController extends MultiActionController{
	
private IQuinielaDao quinielaDao;
	
	public IQuinielaDao getQuinielaDao(){
		return quinielaDao;
	}
	
	public void setQuinielaDao(IQuinielaDao quinielaDao){
		this.quinielaDao = quinielaDao;
	}
	
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

			List<Quiniela> quinieles = quinielaDao.findAll( Order.asc("numJornadaQuiniela") );
			
			Quiniela novaQuiniela = new Quiniela();
					
			ModelMap modelMap = new ModelMap();
			modelMap.addAttribute("llistatQuinieles", quinieles);
			modelMap.put("quiniela", novaQuiniela);
			return new ModelAndView("administracion/quinieles", modelMap);
	}
	
	public ModelAndView hanldeBindException(HttpServletRequest request, HttpServletResponse response, ServletRequestBindingException bindingException) {
	    
		BindException bindException = (BindException) bindingException.getRootCause();
		BindingResult bindingResult = (BindingResult) bindException.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");
		String error = bindingResult.getFieldError().getDefaultMessage();
		
		EscrutiniBackBean escrutini = (EscrutiniBackBean)bindingResult.getTarget();

		Quiniela quiniela = quinielaDao.findById(escrutini.getIdQuiniela());
				
		ModelMap modelMap = new ModelMap();
		modelMap.put("escrutini", escrutini);
		modelMap.put("quiniela", quiniela);
		modelMap.put("error", error);
		return new ModelAndView("administracion/formEscrutini", modelMap);			
		
	}
	
	public ModelAndView showFormEscrutini(HttpServletRequest request,
			HttpServletResponse response){
		
		Long idQuiniela = Long.parseLong(request.getParameter("idQuiniela"));
		Quiniela quiniela = quinielaDao.findById(idQuiniela);
		
		EscrutiniBackBean escrutini = new EscrutiniBackBean();
		escrutini.setNumEncerts10(quiniela.getNumEncerts10());
		escrutini.setNumEncerts11(quiniela.getNumEncerts11());
		escrutini.setNumEncerts12(quiniela.getNumEncerts12());
		escrutini.setNumEncerts13(quiniela.getNumEncerts13());
		escrutini.setNumEncerts14(quiniela.getNumEncerts14());
		escrutini.setNumEncerts15(quiniela.getNumEncerts15());
		escrutini.setPremi10(quiniela.getPremi10());
		escrutini.setPremi11(quiniela.getPremi11());
		escrutini.setPremi12(quiniela.getPremi12());
		escrutini.setPremi13(quiniela.getPremi13());
		escrutini.setPremi14(quiniela.getPremi14());
		escrutini.setPremi15(quiniela.getPremi15());
		escrutini.setRecaudacio(quiniela.getRecaudacio());
		escrutini.setPot(quiniela.getPot());
		
		ModelMap model = new ModelMap();
		model.put("quiniela", quiniela);
		model.put("escrutini", escrutini);
		return new ModelAndView("administracion/formEscrutini", model);
	}
	
	public ModelAndView addEscrutini(HttpServletRequest request,
			HttpServletResponse response, EscrutiniBackBean escrutini){
		
		Long idQuiniela = Long.parseLong(request.getParameter("idQuiniela"));
		Quiniela quiniela = quinielaDao.findById(idQuiniela);
		
		quiniela.setNumEncerts15(escrutini.getNumEncerts15());
		quiniela.setNumEncerts14(escrutini.getNumEncerts14());
		quiniela.setNumEncerts13(escrutini.getNumEncerts13());
		quiniela.setNumEncerts12(escrutini.getNumEncerts12());
		quiniela.setNumEncerts11(escrutini.getNumEncerts11());
		quiniela.setNumEncerts10(escrutini.getNumEncerts10());
		
		Float premi15 = escrutini.getPremi15();
		Float premi14 = escrutini.getPremi14();
		Float premi13 = escrutini.getPremi13();
		Float premi12 = escrutini.getPremi12();
		Float premi11 = escrutini.getPremi11();
		Float premi10 = escrutini.getPremi10();
		Float recaudacio = escrutini.getRecaudacio();
		Float pot = escrutini.getPot();
		
		quiniela.setPremi15(premi15);		
		quiniela.setPremi14(premi14);
		quiniela.setPremi13(premi13);
		quiniela.setPremi12(premi12);
		quiniela.setPremi11(premi11);
		quiniela.setPremi10(premi10);
		quiniela.setRecaudacio(recaudacio);
		quiniela.setPot(pot);
		
		quinielaDao.save(quiniela);
		
		return new ModelAndView("redirect:init.htm");
		
	}

}
